package com.sugon.controller.basics;

import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.FaultTypeEntity;
import com.sugon.service.FaultTypeService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 故障类型Controller
 *
 * @author sugon
 * @date 2019-07-11 10:11:38
 */
@RestController
@RequestMapping("faulttype")
public class FaultTypeController {

    @Autowired
    private FaultTypeService faultTypeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<FaultTypeEntity> faultTypeList = faultTypeService.queryList(query);
        int total = faultTypeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(faultTypeList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);

       /* Map<String, Object> map = new HashMap<>();

        List<FaultTypeEntity> faultList = faultTypeService.queryList(map);

        return R.ok().put("list", faultList);*/
    }

    /**
     * 选择故障类型
     */
    @RequestMapping("/select")
    public R select(@RequestParam Map<String, Object> params) {

        List<FaultTypeEntity> faultList = faultTypeService.queryList(params);

        return R.ok().put("faultList", faultList);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        FaultTypeEntity faultType = faultTypeService.queryObject(id);

        return R.ok().put("faultType", faultType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("faulttype:save")
    @ResponseBody
    public R save(@RequestBody FaultTypeEntity faultType) {
        // 模块类型必填！
        if (faultType.getCategory() == null) {
            return R.error("模块类型必选！");
        }
        // 故障名称不允许重复
        List<FaultTypeEntity> existsList = faultTypeService.queryList(faultType.getCategory(), null, faultType.getTypeName());
        if (CollectionUtil.isNotEmpty(existsList)) {
            return R.error("当前故障类型下，故障名称已存在，请确认！");
        }
        // 故障名称不允许重复
        existsList = faultTypeService.queryList(faultType.getCategory(), faultType.getFaultCode(), null);
        if (CollectionUtil.isNotEmpty(existsList)) {
            return R.error("当前故障类型下，故障代码已存在，请确认！");
        }
        return faultTypeService.save(faultType) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("faulttype:update")
    @ResponseBody
    public R update(@RequestBody FaultTypeEntity faultType) {
        faultTypeService.update(faultType);
        return R.ok();
    }

    @RequestMapping("/updateDel/{id}")
    @RequiresPermissions("faulttype:delete")
    @ResponseBody
    public R updateDel(@PathVariable("id") String id) {
        //判断是否有下级
        List<String> list = faultTypeService.queryPidList(id);
        if (list.size() > 0) {
            return R.error("请先删除子故障类型");
        }

        faultTypeService.updateDel(id);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("faulttype:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        faultTypeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<FaultTypeEntity> list = faultTypeService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * excel导入
     */
    @RequestMapping("/import")
    @ResponseBody
    public R importE(@RequestParam("file") MultipartFile file) throws Exception {
        String err = faultTypeService.importE(file);

        return R.ok().put("err", err);
    }

    /**
     * 选择故障类型
     */
    @RequestMapping("/selectByType")
    @ResponseBody
    public R selectByType(Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("category", type);
        List<FaultTypeEntity> faultList = faultTypeService.queryList(map);

        return R.ok().put("faultList", faultList);
    }
}
